_realname=spice
_longname=${_realname}-server
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.14.3
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
pkgdesc="SPICE: Simple Protocol for Independent Computing Environments"
license=("LGPLv2.1")
url="https://www.spice-space.org/"
depends=(
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-gstreamer"
             "${MINGW_PACKAGE_PREFIX}-gst-plugins-base"
             "${MINGW_PACKAGE_PREFIX}-gst-plugins-good"
             "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
             "${MINGW_PACKAGE_PREFIX}-lz4"
             "${MINGW_PACKAGE_PREFIX}-openssl"
             "${MINGW_PACKAGE_PREFIX}-opus"
             "${MINGW_PACKAGE_PREFIX}-pixman"
             "${MINGW_PACKAGE_PREFIX}-spice-protocol"
             "${MINGW_PACKAGE_PREFIX}-zlib"
)
optdepends=(
             "${MINGW_PACKAGE_PREFIX}-gst-plugins-ugly: for the x264enc GStreamer element"
             "${MINGW_PACKAGE_PREFIX}-gst-libav:        for the avenc_mjpeg GStreamer element"
)
makedepends=(
             "${MINGW_PACKAGE_PREFIX}-asciidoc"
             "${MINGW_PACKAGE_PREFIX}-gcc"
)
source=(https://www.spice-space.org/download/releases/${_longname}/${_realname}-${pkgver}.tar.bz2{,.sign})
validpgpkeys=('206D3B352F566F3B0E6572E997D9123DE37A484F')
sha256sums=('551d4be4a07667cf0543f3c895beb6da8a93ef5a9829f2ae47817be5e616a114' 'SKIP')

prepare() {
  # Fix test to recognize CRLF lineending
  find "${srcdir}" -name test-logging.c -exec \
    sed -i "s/\\\\n/\\\\r\\\\n/g" {} \;
  # Deactivate failing tests
  find "${srcdir}" -name test-qxl-parsing.c -exec \
    sed -i 's%\(g_test_add_func("/server/qxl-parsing\)%\/\/\1%' {} \;
}

build() {
  [[ -d "${srcdir}"/build-${MINGW_CHOST} ]] && rm -rf "${srcdir}"/build-${MINGW_CHOST}

  mkdir -p "${srcdir}"/build-${MINGW_CHOST} && cd "${srcdir}"/build-${MINGW_CHOST}

  # --enable-extra-checks - adds failing test-video-encoders
  # --enable-manual=yes - a2x/asciidoc fails and manual is already generated in src
  # --with-sasl=yes - compilation fails
  # --enable-lz4=yes - linking fails on i686
  [ "i686" == "${CARCH}" ] && lz4=no || lz4=yes
  CFLAGS="-g -O2 -fstack-protector" \
  LDFLAGS="-g" \
  "${srcdir}"/${_realname}-${pkgver}/configure \
    --prefix="${MINGW_PREFIX}" \
    --disable-silent-rules \
    --enable-lz4=${lz4} \
    --enable-gstreamer=1.0 \
    --enable-manual=no \
    --with-sasl=no

  make
}

check() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make check
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}
  make install DESTDIR="${pkgdir}"

  cd "${srcdir}"/${_realname}-${pkgver}
  local DOCDIR="${pkgdir}"/${MINGW_PREFIX}/share/doc/${_realname}
  local LICENSEDIR="${pkgdir}"/${MINGW_PREFIX}/share/licenses/${_realname}
  mkdir -p $DOCDIR $LICENSEDIR
  cp -p COPYING $LICENSEDIR/
  cp -a README AUTHORS CHANGELOG.md docs/images docs/*.html \
    docs/manual/images docs/manual/*.html \
    docs/manual/manual.chunked \
    $DOCDIR/
}
